-- Btext, a text creator using blocks for Minetest   
--	Copyright (C) 2018 Neoniet neoniet AT neoniet.com
--
--	This program is free software: you can redistribute it and/or modify
--	it under the terms of the GNU General Public License as published by
--	the Free Software Foundation, either version 3 of the License, or
--	(at your option) any later version.
--	
--	This program is distributed in the hope that it will be useful,
--	but WITHOUT ANY WARRANTY; without even the implied warranty of
--	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--	GNU General Public License for more details.
--	
--	You should have received a copy of the GNU General Public License
--	along with this program.  If not, see <https://www.gnu.org/licenses/>.
--


function btext.printChar(pos,degrees,char,foreground,background)
	
	local abc = btext.fonts.dina
	char = char:upper()
	if char == ' ' then
		char = "space"
	end
	
	-- Depending on the orientation we have to modify x or z 
	-- to maintain the correct direction of the writing	

	-- North x++ 
	if degrees >= 321 or degrees <= 45 then	
		for y=1,abc.height do
			for x=1,abc.width do
				if abc[char][y-1][x-1] == 1 then
					minetest.set_node({x=x+pos.x,y=pos.y+y,z=pos.z}, { name = foreground })
				else
					minetest.set_node({x=x+pos.x,y=pos.y+y,z=pos.z}, { name = background })
				end
			end
		end
	-- West  z++
	elseif degrees >=46 and degrees <=135 then
		for y=1,abc.height do
			for x=1,abc.width do
				if abc[char][y-1][x-1] == 1 then
					minetest.set_node({x=pos.x,y=pos.y+y,z=pos.z+x}, { name = foreground })
				else
					minetest.set_node({x=pos.x,y=pos.y+y,z=pos.z+x}, { name = background })
				end
			end
		end
	-- South
	elseif degrees >=136 and degrees <=225 then
		for y=1,abc.height do
			for x=1,abc.width do
				if abc[char][y-1][x-1] == 1 then
					minetest.set_node({x=pos.x-x,y=pos.y+y,z=pos.z}, { name = foreground })
				else
					minetest.set_node({x=pos.x-x,y=pos.y+y,z=pos.z}, { name = background })
				end
			end
		end
	-- East
	elseif degrees >=226 and degrees <=320 then
		for y=1,abc.height do
			for x=1,abc.width do
				if abc[char][y-1][x-1] == 1 then
					minetest.set_node({x=pos.x,y=pos.y+y,z=pos.z-x}, { name = foreground })
				else
					minetest.set_node({x=pos.x,y=pos.y+y,z=pos.z-x}, { name = background })
				end
			end
		end
	end
end

function btext.printString(pos,degrees,message, foreground,background)
	local abc = btext.fonts.dina
	local string_size = message:len()
	
	-- Depending on the orientation we have to modify x or z 
	-- to maintain the correct direction of the writing	

	for c=1,string_size do
		btext.printChar(pos,degrees,message:sub(c,c), foreground, background)
		-- North x++ 
		if degrees >= 321 or degrees <= 45 then	
			pos.x = pos.x + abc.width
		-- West  z++
		elseif degrees >=46 and degrees <=135 then
			pos.z = pos.z + abc.width
		-- South x--
		elseif degrees >=136 and degrees <=225 then
			pos.x = pos.x - abc.width
		-- East z--
		elseif degrees >=226 and degrees <=320 then
			pos.z = pos.z - abc.width
		end
	end
end

